package com.jming.jike.config;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * MVC 配置
 *
 * @author jming
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/*")
                .addResourceLocations("classpath:/static/",
                        "classpath:/META-INF/resources/webjars/",
                        "classpath:/META-INF/resources/");

        // 解决 SWAGGER2 404报错 /swagger-ui/index.html
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");

    }

//    /**
//     * extends WebMvcConfigurationSupport
//     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        GsonBuilder gb = new GsonBuilder();
//        gb.enableComplexMapKeySerialization();
//        gb.setDateFormat("yyyy-MM-dd HH:mm:ss");
//        gb.serializeNulls();
//        //swagger出错
//        gb.registerTypeAdapter(Json.class, (JsonSerializer<Json>) (json, type, jsonSerializationContext) ->
//                JsonParser.parseString(json.value()));
//
//        // Long转换为字符串
//        gb.registerTypeAdapter(Long.class, (JsonSerializer<Long>) (aLong, type, jsonSerializationContext) ->
//                aLong == null ? null : new JsonPrimitive(aLong.toString()));
//
//        GsonHttpMessageConverter gson = new GsonHttpMessageConverter();
//        gson.setGson(gb.create());
//        converters.add(gson);
//
//        super.configureMessageConverters(converters);
//    }

    @Override
    public void extendMessageConverters(@NotNull List<HttpMessageConverter<?>> converters) {

        if (!CollectionUtils.isEmpty(converters)) {
            converters.forEach(httpMessageConverter -> {
                if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) {

                    // 的到对象映射器
                    ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter) httpMessageConverter).getObjectMapper();

                    // 设置对象转换器, 使用Jackson将Java对象转为json
                    objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
                    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                    SimpleModule simpleModule = new SimpleModule();

                    // 处理Long型数据 确保前端精度缺失
                    simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
                    simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
                    simpleModule.addSerializer(long.class, ToStringSerializer.instance);

                    // 处理时间格式数据
                    simpleModule.addSerializer(Date.class, new DateSerializer(false,
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
                    objectMapper.registerModule(simpleModule);
                }
            });
        }
    }


}
